<?php  
## include required files
/*******************************/
require_once '../model/forums.php';

/*******************************/
## Create Objects
/*******************************/
$forumObj = new Model_Forums();
/*******************************/
if(isset($_POST['editforum_btn']) || $_POST['title']) {
	## apply PHP validation for required filed
	if(trim($_POST['title']) != '' && trim($_POST['description']) != '') {
		
		$forumArray = array();
		extract($_POST);
		//********************************************UPlOADING PHOTO*************************************************************//
		if($_FILES['forum_image']['name']!='') {
				
				$size =	getimagesize($_FILES['forum_image']['tmp_name']);
				
				if($_FILES['forum_image']['name']!= '' && ($size[0]>=FORUMIMAGEWIDTH && $size[1]>=FORUMIMAGEHEIGHT) ) {
					
				##unlink the prevoius image
				$forum_image = '../uploads/forum/'.$_POST['hidden_image']; 
				@unlink($forum_image);
				
				$path = '../uploads/forum/';
				$imageName    = $_FILES['forum_image']['name'];
				$arrImageName = explode(".",$imageName);
				$position     = count($arrImageName);
				$fileName     = date('Ymdhis').".".$arrImageName[$position-1];
				copy($_FILES["forum_image"]["tmp_name"],$path.$fileName);
				$forumArray['image']=$fileName;
				} else {
					$_SESSION['msg'] = "<div class='error_msg'><span>".FORUMIMAGE_WIDTHHEIGHT."</span></div>";
				}	
			}		
			
			if($_SESSION['msg'] =='') {
			$forum_id=base64_decode($_GET['id']);	
			$forumArray['title'] = return_post_value($_POST['title']);
			$forumArray['description'] = return_post_value($description);
			$forumArray['user_id'] = 1;
			$forumArray['added_date'] = date('Y-m-d');
			$forumObj->editForumById($forumArray,$forum_id);
			$_SESSION['msg'] = "<div class='success_msg'><span>Forum edited successfully</span></div>";
		    header('location:'.SITE_URL.'/admin/home.php?q=forums&page='.$_POST['page']);
		    exit;
			}
       //************************************************************************************************************
	} else {// php validation else
		$_SESSION['msg'] = "<div class='error_msg'><span>Please enter Forum title and description</span></div>";
	}
	// main if closed
}

if($_GET['id']!=''&& $_GET['action']=='edit') {
	$forum_id=base64_decode($_GET['id']);
	$smarty->assign('page', $_GET['page']);
	$forumArray=$forumObj->getForumsDetailsById($forum_id);
	
	$smarty->assign('forumArray', $forumArray);
}
## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'forums');
$smarty->assign('mainmenu', '3');
## Unset all the objects created which are on this page
unset($forumObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'forums/editforum.tpl');
unset($smarty);
?>